package com.csot.ry.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.net.Ipv4Util;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.csot.common.core.domain.entity.SysUser;
import com.csot.common.core.text.Convert;
import com.csot.common.exception.ServiceException;
import com.csot.common.utils.DateUtils;
import com.csot.common.utils.SecurityUtils;
import com.csot.common.utils.StringUtils;
import com.csot.config.Caches;
import com.csot.model.DcnSegment;
import com.csot.ry.domain.TDcnCity;
import com.csot.ry.util.CacheKitUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.csot.ry.mapper.TDcnSegmentMapper;
import com.csot.ry.domain.TDcnSegment;
import com.csot.ry.service.ITDcnSegmentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * DCNService业务层处理
 * 
 * @author csot
 * @date 2023-04-03
 */
@Service
public class TDcnSegmentServiceImpl implements ITDcnSegmentService 
{
    private static final Logger log = LoggerFactory.getLogger(TDcnSegmentServiceImpl.class);
    @Autowired
    private TDcnSegmentMapper tDcnSegmentMapper;

    /**
     * 查询DCN
     * 
     * @param id DCN主键
     * @return DCN
     */
    @Override
    public TDcnSegment selectTDcnSegmentById(Long id)
    {
        return tDcnSegmentMapper.selectTDcnSegmentById(id);
    }

    /**
     * 查询DCN列表
     * 
     * @param tDcnSegment DCN
     * @return DCN
     */
    @Override
    public List<TDcnSegment> selectTDcnSegmentList(TDcnSegment tDcnSegment)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptid = user.getDeptId();
        if (deptid != 100) {
            tDcnSegment.setIpCity(CacheKitUtil.ip_dept.get(user.getDept().getParentId() + ""));
        }
        try {
            if (StrUtil.isNotEmpty(tDcnSegment.getIpStart())) {
                tDcnSegment.setIpStartI(NetUtil.ipv4ToLong(tDcnSegment.getIpStart().trim()));
            }
            if (StrUtil.isNotEmpty(tDcnSegment.getIpEnd())) {
                tDcnSegment.setIpEndI(NetUtil.ipv4ToLong(tDcnSegment.getIpEnd().trim()));
            }
        } catch (Exception e) {
            log.error("起止IP不正确", e);
            throw new IllegalArgumentException("起止IP不正确");
        }
        return tDcnSegmentMapper.selectTDcnSegmentList(tDcnSegment);
    }

    /**
     * 新增DCN
     * 
     * @param tDcnSegment DCN
     * @return 结果
     */
    @Override
    public int insertTDcnSegment(TDcnSegment tDcnSegment)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!"admin".equals(user.getUserName())) {
            Long deptid = user.getDeptId();
            String pdeptname = CacheKitUtil.ip_dept.get(String.valueOf(user.getDept().getParentId()));
            if (deptid != 100 && StrUtil.isNotEmpty(tDcnSegment.getIpCity())
                    && !tDcnSegment.getIpCity().equals(pdeptname)) {
                throw new ServiceException("不能创建其它地市的信息");
            }
        }

        tDcnSegment.setCreateTime(DateUtils.getNowDate());
        tDcnSegment.setCreateBy(SecurityUtils.getUsername());
        tDcnSegment.setIpStartI(NetUtil.ipv4ToLong(tDcnSegment.getIpStart().trim()));
        tDcnSegment.setIpEndI(NetUtil.ipv4ToLong(tDcnSegment.getIpEnd().trim()));
        if (tDcnSegment.getIpEndI() < tDcnSegment.getIpStartI()) {
            throw new IllegalArgumentException("起始地址不能大于终止地址。");
        }
        if(StrUtil.isEmpty(tDcnSegment.getIpStatus()) || tDcnSegment.getIpStatus().equals("0")){
            //不传设置为使用
            tDcnSegment.setIpStatus("1");
        }
        if (isExistDcn(tDcnSegment)) {
            throw new ServiceException(
                    String.format("该地址段在数据库中已存在"));
        }

        int result = tDcnSegmentMapper.selectDcnIfExist(tDcnSegment);
        if (result > 0) {
            throw new ServiceException("插入失败：此地址范围已存在");
        }
        return tDcnSegmentMapper.insertTDcnSegment(tDcnSegment);
    }

    /**
     * 修改DCN
     * 
     * @param tDcnSegment DCN
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTDcnSegment(TDcnSegment tDcnSegment)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!"admin".equals(user.getUserName())) {
            Long deptid = user.getDeptId();
            String pdeptname = CacheKitUtil.ip_dept.get(String.valueOf(user.getDept().getParentId()));
            if (deptid != 100 && StrUtil.isNotEmpty(tDcnSegment.getIpCity())
                    && !tDcnSegment.getIpCity().equals(pdeptname)) {
                throw new ServiceException("不能修改其它地市的信息");
            }
        }
        tDcnSegment.setUpdateTime(DateUtils.getNowDate());
        tDcnSegment.setUpdateBy(SecurityUtils.getUsername());
        long newIpStartI = NetUtil.ipv4ToLong(tDcnSegment.getIpStart().trim());
        long newIpEndI = NetUtil.ipv4ToLong(tDcnSegment.getIpEnd().trim());
        tDcnSegment.setIpEndI(newIpEndI);
        tDcnSegment.setIpStartI(newIpStartI);
        if (newIpEndI < newIpStartI) {
            throw new ServiceException("终止IP不能小于起始IP！");
        }
        if(StrUtil.isEmpty(tDcnSegment.getIpStatus())){
            //不传设置为使用
            tDcnSegment.setIpStatus("1");
        }
        //全量表是否重复
        if (isExistDcnForEdit(tDcnSegment)) {
            throw new ServiceException(
                    String.format("该地址段在数据库中已存在"));
        }
        //是否小于子表数据
        if(isExistDcnCity(tDcnSegment.getIpStartI(),tDcnSegment.getIpEndI(),tDcnSegment.getIpCity())){
            throw new ServiceException(
                    String.format("全量表IP数据不能小于明细表IP数据"));
        }

        return tDcnSegmentMapper.updateTDcnSegment(tDcnSegment);
    }

    /**
     * 批量删除DCN
     * 
     * @param ids 需要删除的DCN主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTDcnSegmentByIds(Long[] ids)
    {
        //查询Ip和地市
        List<Record> dcnList = Db.find(
                    "select * from t_dcn_segment where id in ("+ ArrayUtil.join(ids,",") +")");
        if(null != dcnList && dcnList.size()>0){
            for(int i = 0; i<dcnList.size();i++){
                Record td = dcnList.get(i);
                if(isExistDcnCity(td.getLong("ip_start_i"),td.getLong("ip_end_i"),td.getStr("ip_city"))){
                    throw new ServiceException(
                            String.format("明细表中已存在，请先删除明细表数据"));
                }
            }
        }
        //判断子表中是否存在，存在不让删除
        return tDcnSegmentMapper.deleteTDcnSegmentByIds(ids);
    }

    /**
     * 删除DCN信息
     * 
     * @param id DCN主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTDcnSegmentById(Long id)
    {
        return tDcnSegmentMapper.deleteTDcnSegmentById(id);
    }
    @Override
    @Transactional
    public int importDcnSegment(List<TDcnSegment> tDcnSegmentList){
        if (CollUtil.isEmpty(tDcnSegmentList)) {
            throw new ServiceException("导入数据不能为空！");
        }
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptid = user.getDeptId();
        Long pdeptid = deptid;
        if (deptid != 100) {
            pdeptid = user.getDept().getParentId();
        }
//        String city_e = CacheKitUtil.ip_dept.get(String.valueOf(pdeptid));
        String city_zh = Caches.getDeptName(String.valueOf(pdeptid));

        for (int i = 0; i < tDcnSegmentList.size(); i++) {
            TDcnSegment tcs = tDcnSegmentList.get(i);
            try {
                if (tcs.getIpStart().contains("/")) {
                    String[] ips = tcs.getIpStart().split("/");
                    tcs.setIpStart(ips[0].trim());
                    tcs.setIpStartI(NetUtil.ipv4ToLong(ips[0].trim()));
                    tcs.setIpEndI(Ipv4Util.getEndIpLong(ips[0].trim(), Convert.toInt(ips[1])));
                    tcs.setIpEnd(NetUtil.longToIpv4(tcs.getIpEndI()));
                    tcs.setIpEndI(NetUtil.ipv4ToLong(tcs.getIpEnd()));
                } else {
                    tcs.setIpStart(tcs.getIpStart().trim());
                    tcs.setIpEnd(tcs.getIpEnd().trim());
                    tcs.setIpStartI(NetUtil.ipv4ToLong(tcs.getIpStart().trim()));
                    tcs.setIpEndI(NetUtil.ipv4ToLong(tcs.getIpEnd().trim()));
                }

//                `tcs.setIpStart(tcs.getIpStart().trim());
//                tcs.setIpEnd(tcs.getIpEnd().trim());
//                tcs.setIpStartI(NetUtil.ipv4ToLong(tcs.getIpStart().trim()));
//                tcs.setI`pEndI(NetUtil.ipv4ToLong(tcs.getIpEnd().trim()));

            } catch (Exception e) {
                log.error(e.getMessage());
                throw new ServiceException(
                        String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2, tcs.getIpStart(), "数据格式不正确"));
            }
            if (StringUtils.isBlank(tcs.getIpCity())) {
                throw new ServiceException(
                        String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2, tcs.getIpStart(), "地市不能为空"));
            }
            if (deptid != 100) {
                if (!tcs.getIpCity().equals(city_zh)) {
                    throw new ServiceException(String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2, tcs.getIpStart(),
                            "只能上传'" + city_zh + "'市数据"));
                }
            }
            String city_e = CacheKitUtil.ip_city.getKey(tcs.getIpCity());
            tcs.setIpCity(city_e);
            if (isExistDcn(tcs)) {
                throw new ServiceException(
                        String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2, tcs.getIpStart(), "该地址段部分已导入"));
            }
            if (StringUtils.isNotEmpty(tcs.getIpUseType())) {
                tcs.setIpUseType(CacheKitUtil.dcn_use_type.getKey(tcs.getIpUseType()));
            }
            if (StrUtil.isEmpty(tcs.getIpStatus())) {
                tcs.setIpStatus("1");
            } else {
                tcs.setIpStatus(CacheKitUtil.dcn_use_status.getKey(tcs.getIpStatus()));
            }
            if (StrUtil.isNotEmpty(tcs.getDeviceType())) {
                tcs.setDeviceType(CacheKitUtil.device_type.getKey(tcs.getDeviceType()));
            }
            tcs.setCreateTime(new Date());
            tcs.setCreateBy(user.getUserName());
        }

        for (int i = 0; i < tDcnSegmentList.size(); i++) {
            TDcnSegment tcs1 = tDcnSegmentList.get(i);
            for (int j = i + 1; j < tDcnSegmentList.size(); j++) {
                TDcnSegment tcs2 = tDcnSegmentList.get(j);
                if (tcs1.getIpStartI() > tcs2.getIpStartI() && tcs1.getIpStartI() < tcs2.getIpEndI()) {
                    throw new ServiceException(String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2, tcs1.getIpStart(),
                            "起始地址和第" + (j + 2) + "行有重复"));
                }
                if (tcs1.getIpEndI() < tcs2.getIpEndI() && tcs1.getIpEndI() > tcs2.getIpStartI()) {
                    throw new ServiceException(String.format("第%d行数据异常，终止地址为：%s,异常原因：%s", i + 2, tcs1.getIpEnd(),
                            "终止地址和第" + (j + 2) + "行有重复"));
                }
                if (tcs1.getIpStartI() <= tcs2.getIpStartI() && tcs1.getIpEndI() >= tcs2.getIpEndI()) {
                    throw new ServiceException(String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2, tcs1.getIpStart(),
                            "起始地址和第" + (j + 2) + "行有重复"));
                }
            }
        }
        for (int i = 0; i < tDcnSegmentList.size(); i++) {
            try {
                tDcnSegmentMapper.insertTDcnSegment(tDcnSegmentList.get(i));
            } catch (Exception e) {
                log.error("", e);
                throw new ServiceException(String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2,
                        tDcnSegmentList.get(i).getIpStart(), "数据无法插入数据库，请联系管理员"));
            }
        }

        return tDcnSegmentList.size();
    }

    @Override
//    @Transactional
    public synchronized int importEdit(List<TDcnSegment> tCitySegmentList) {
        DcnSegment csdao = new DcnSegment().dao();
        if (CollUtil.isEmpty(tCitySegmentList)) {
            throw new ServiceException("地市数据不能为空！");
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long deptid = user.getDeptId();
        Long pdeptid = deptid;
        if (deptid != 100) {
            pdeptid = user.getDept().getParentId();
        }
        String city_e = CacheKitUtil.ip_dept.get(String.valueOf(pdeptid));
        String city_zh = Caches.getDeptName(String.valueOf(pdeptid));

        for (int i = 0; i < tCitySegmentList.size(); i++) {
            TDcnSegment tcs = tCitySegmentList.get(i);
            try {

                tcs.setIpStart(tcs.getIpStart().trim());
                if (tcs.getIpStart().contains("/")) {
                    String[] ips = tcs.getIpStart().split("/");
                    tcs.setIpStart(ips[0].trim());
                    tcs.setIpStartI(NetUtil.ipv4ToLong(ips[0].trim()));
                    tcs.setIpEndI(Ipv4Util.getEndIpLong(ips[0].trim(), Convert.toInt(ips[1])));
                    tcs.setIpEnd(NetUtil.longToIpv4(tcs.getIpEndI()));
                    tcs.setIpEndI(NetUtil.ipv4ToLong(tcs.getIpEnd()));
                } else {
                    tcs.setIpEnd(tcs.getIpEnd().trim());
                    tcs.setIpStartI(NetUtil.ipv4ToLong(tcs.getIpStart().trim()));
                    tcs.setIpEndI(NetUtil.ipv4ToLong(tcs.getIpEnd().trim()));
                }

            } catch (Exception e) {
                log.error(e.getMessage());
                throw new ServiceException(
                        String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2, tcs.getIpStart(), "数据格式不正确"));
            }
            if (StringUtils.isBlank(tcs.getIpCity())) {
                throw new ServiceException(
                        String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2, tcs.getIpStart(), "地市不能为空"));
            }
            if (deptid != 100) {
                if (!tcs.getIpCity().equals(city_zh)) {
                    throw new ServiceException(String.format("第%d行数据异常，起始地址为：%s,异常原因：%s", i + 2, tcs.getIpStart(),
                            "只能上传'" + city_zh + "'市数据"));
                }
            }

            if (deptid == 100) {
                city_e = CacheKitUtil.ip_city.getKey(tcs.getIpCity());
            }

            tcs.setCreateTime(new Date());
            tcs.setCreateBy(user.getUserName());
            DcnSegment citySegment = csdao.findFirst("select * from t_dcn_segment \n" +
                            " where ip_start_i <= ? and ip_end_i >= ?  and ip_city = ? ",
                    tcs.getIpStartI(), tcs.getIpEndI(), city_e);

            if(null == citySegment){
                throw new ServiceException(String.format("第%d行数据异常，终止地址为：%s,异常原因：%s", i + 2, tcs.getIpEnd(),
                        "表中无些地址段数据"));
            }
            //举例说明：原地址段：10.1.2.0 - 10.1.2.255  未使用 。。。。。。
            //  根据excel表格中导入的地址段信息修改10.1.2.10 - 10.1.2.20  为 地址池 。。。。。。
            //其他剩下的地址段保持原来信息不变。
            //修改后的结果：10.1.2.0 - 10.1.2.9  未使用 。。。。。。
            //  10.1.2.10 - 10.1.2.20  地址池 。。。。。。
            //  10.1.2.21 - 10.1.2.255 未使用 。。。。。。

            TDcnSegment newTCitySegment =  new TDcnSegment();
            TDcnSegment newTCitySegmentUpBegin =  new TDcnSegment();
            TDcnSegment newTCitySegmentUpEnd =  new TDcnSegment();
            //相等
            //b=? && e=?
            if(NumberUtil.compare(citySegment.getLong("ip_start_i"),tcs.getIpStartI()) == 0 &&
                    NumberUtil.compare(citySegment.getLong("ip_end_i"),tcs.getIpEndI()) ==0 ){
                BeanUtils.copyProperties(citySegment,newTCitySegment);
//                newTCitySegment.setIpUseType(CacheKitUtil.dcn_use_type.getKey(tcs.getIpUseType()));
                setTDcnCity(newTCitySegment,tcs);
                tDcnSegmentMapper.updateTDcnSegment(newTCitySegment);
            }else  if(NumberUtil.compare(citySegment.getLong("ip_start_i"),tcs.getIpStartI()) == 0 &&
                    NumberUtil.compare(citySegment.getLong("ip_end_i"),tcs.getIpEndI()) == 1 ){
                //b=? && e>?
                BeanUtils.copyProperties(citySegment,newTCitySegment);
                BeanUtils.copyProperties(citySegment,newTCitySegmentUpEnd);
                newTCitySegment.setIpEnd(tcs.getIpEnd());
                newTCitySegment.setIpEndI(tcs.getIpEndI());
//                newTCitySegment.setIpUseType(CacheKitUtil.dcn_use_type.getKey(tcs.getIpUseType()));
                setTDcnCity(newTCitySegment,tcs);
                tDcnSegmentMapper.updateTDcnSegment(newTCitySegment);

                newTCitySegmentUpEnd.setIpStartI(tcs.getIpEndI()+1);
                newTCitySegmentUpEnd.setIpStart(NetUtil.longToIpv4(newTCitySegmentUpEnd.getIpStartI()));
                tDcnSegmentMapper.insertTDcnSegment(newTCitySegmentUpEnd);
            }else  if(NumberUtil.compare(citySegment.getLong("ip_start_i") , tcs.getIpStartI())== -1 &&
                    NumberUtil.compare(citySegment.getLong("ip_end_i") , tcs.getIpEndI())== 0){
                //b<? && e=?
                BeanUtils.copyProperties(citySegment,newTCitySegment);
                BeanUtils.copyProperties(citySegment,newTCitySegmentUpBegin);

                newTCitySegment.setIpStartI(tcs.getIpStartI());
                newTCitySegment.setIpStart(tcs.getIpStart());
//                newTCitySegment.setIpUseType(CacheKitUtil.dcn_use_type.getKey(tcs.getIpUseType()));
                setTDcnCity(newTCitySegment,tcs);
                tDcnSegmentMapper.updateTDcnSegment(newTCitySegment);

                newTCitySegmentUpBegin.setIpEndI(tcs.getIpStartI()-1);
                newTCitySegmentUpBegin.setIpEnd(NetUtil.longToIpv4(newTCitySegmentUpBegin.getIpEndI()));
                tDcnSegmentMapper.insertTDcnSegment(newTCitySegmentUpBegin);
            }else  if(NumberUtil.compare(citySegment.getLong("ip_start_i"), tcs.getIpStartI())== -1 &&
                    NumberUtil.compare(citySegment.getLong("ip_end_i") , tcs.getIpEndI())== 1){
                //b<? && e>?
                BeanUtils.copyProperties(citySegment,newTCitySegment);
                BeanUtils.copyProperties(citySegment,newTCitySegmentUpEnd);
                BeanUtils.copyProperties(citySegment,newTCitySegmentUpBegin);

                newTCitySegment.setIpStartI(tcs.getIpStartI());
                newTCitySegment.setIpStart(tcs.getIpStart());
                newTCitySegment.setIpEnd(tcs.getIpEnd());
                newTCitySegment.setIpEndI(tcs.getIpEndI());
//                newTCitySegment.setIpUseType(CacheKitUtil.dcn_use_type.getKey(tcs.getIpUseType()));
                setTDcnCity(newTCitySegment,tcs);
                tDcnSegmentMapper.updateTDcnSegment(newTCitySegment);

                newTCitySegmentUpBegin.setIpEndI(tcs.getIpStartI()-1);
                newTCitySegmentUpBegin.setIpEnd(NetUtil.longToIpv4(newTCitySegmentUpBegin.getIpEndI()));
                tDcnSegmentMapper.insertTDcnSegment(newTCitySegmentUpBegin);

                newTCitySegmentUpEnd.setIpStartI(tcs.getIpEndI()+1);
                newTCitySegmentUpEnd.setIpStart(NetUtil.longToIpv4(newTCitySegmentUpEnd.getIpStartI()));
                tDcnSegmentMapper.insertTDcnSegment(newTCitySegmentUpEnd);
            }

        }
        return tCitySegmentList.size();
    }

    //地市不能修改
    private void setTDcnCity(TDcnSegment dbTDcnCity, TDcnSegment webTDcnCity){
        dbTDcnCity.setIpUseType(CacheKitUtil.dcn_use_type.getKey(webTDcnCity.getIpUseType()));
        if(StrUtil.isNotEmpty(webTDcnCity.getIpUseDescribe())){
            dbTDcnCity.setIpUseDescribe(webTDcnCity.getIpUseDescribe());
        }
        if(StrUtil.isNotEmpty(webTDcnCity.getDeviceType())){
            dbTDcnCity.setDeviceType(CacheKitUtil.device_type.getKey(webTDcnCity.getDeviceType()));
        }
        if(StrUtil.isNotEmpty(webTDcnCity.getDeviceName())){
            dbTDcnCity.setDeviceName(webTDcnCity.getDeviceName());
        }
        if(StrUtil.isNotEmpty(webTDcnCity.getDeviceAddress())){
            dbTDcnCity.setDeviceAddress(webTDcnCity.getDeviceAddress());
        }
        if(StrUtil.isNotEmpty(webTDcnCity.getIpUser())){
            dbTDcnCity.setIpUser(webTDcnCity.getIpUser());
        }
        if(StrUtil.isNotEmpty(webTDcnCity.getIpManagement())){
            dbTDcnCity.setIpManagement(webTDcnCity.getIpManagement());
        }
        if(StrUtil.isNotEmpty(webTDcnCity.getRemark())){
            dbTDcnCity.setRemark(webTDcnCity.getRemark());
        }
        if(StrUtil.isNotEmpty(webTDcnCity.getIpStatus())){
            dbTDcnCity.setIpStatus(CacheKitUtil.dcn_use_status.getKey(webTDcnCity.getIpStatus()));
        }
    }

    //表内查询,大于0说明此IP在表中已经存在
    private boolean isExistDcnForEdit(TDcnSegment tDcnSegment){
        List<Record> repeatListcity = Db.find(
                "select id from t_dcn_segment where (( ( ip_start_i between ? and ? ) or ( ip_end_i between ? and ? ) ) or "
                        + " ( ip_start_i <= ? and ? <= ip_end_i )) and ip_city = ? and id != ?",
                tDcnSegment.getIpStartI(), tDcnSegment.getIpEndI(),
                tDcnSegment.getIpStartI(), tDcnSegment.getIpEndI(),
                tDcnSegment.getIpStartI(), tDcnSegment.getIpEndI(),
                tDcnSegment.getIpCity(),tDcnSegment.getId());
        return (repeatListcity.size()>0)?true:false;
    }
    //表内查询,大于0说明此IP在表中已经存在
    private boolean isExistDcn(TDcnSegment tDcnSegment){
        List<Record> repeatListcity = Db.find(
                "select id from t_dcn_segment where (( ( ip_start_i between ? and ? ) or ( ip_end_i between ? and ? ) ) or "
                        + " ( ip_start_i <= ? and ? <= ip_end_i )) and ip_city = ? ",
                tDcnSegment.getIpStartI(), tDcnSegment.getIpEndI(),
                tDcnSegment.getIpStartI(), tDcnSegment.getIpEndI(),
                tDcnSegment.getIpStartI(), tDcnSegment.getIpEndI(),
                tDcnSegment.getIpCity());
        return (repeatListcity.size()>0)?true:false;
    }

    //判断子表中是否存在记录
    private boolean isExistDcnCity(Long startI,Long endI,String ipcidy){
        List<Record> dcncity = Db.find(
                "select id from t_dcn_city where (( ( ip_start_i between ? and ? ) or ( ip_end_i between ? and ? ) ) or "
                        + " ( ip_start_i <= ? and ? <= ip_end_i )) and ip_city = ? ",
                startI, endI,
                startI, endI,
                startI, endI,
                ipcidy);
        return dcncity.size()>0?true:false;
    }
}
